package com.hskn.hss.module.carPlanCar.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.module.alarmcar.entity.AlarmCar;
import com.hskn.hss.module.alarmcar.mapper.AlarmCarMapper;
import com.hskn.hss.module.carPlan.vo.JxqWorkPlanDetailByCarVo;
import com.hskn.hss.module.carPlan.vo.JxqWorkPlanDetailVo;
import com.hskn.hss.module.carPlanCar.entity.CarPlanCar;
import com.hskn.hss.module.carPlanCar.mapper.CarPlanCarMapper;
import com.hskn.hss.module.carPlanCar.service.CarPlanCarService;
import com.hskn.hss.module.carPlanCar.to.CarPlanCarTo;
import com.hskn.hss.module.carPlanCar.vo.*;
import com.hskn.hss.module.carviolation.entity.CarViolation;
import com.hskn.hss.module.carviolation.mapper.CarViolationMapper;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.mapper.SysDepartMapper;
import com.hskn.hss.module.jobset.entity.Jobset;
import com.hskn.hss.module.jobset.mapper.JobsetMapper;
import com.hskn.hss.module.oilDevice.domain.StateOilDevice;
import com.hskn.hss.module.oilDevice.mapper.StateOilDeviceMapper;
import com.hskn.hss.module.rptCarWork.entity.RptCarWork;
import com.hskn.hss.module.rptCarWork.mapper.RptCarWorkMapper;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import com.hskn.hss.utils.page.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Slf4j
@Service
public class CarPlanCarServiceImpl extends ServiceImpl<CarPlanCarMapper, CarPlanCar> implements CarPlanCarService {

    @Resource
    private RptCarWorkMapper rptCarWorkMapper;
    @Resource
    private SysDepartMapper sysDepartMapper;
    @Resource
    private JobsetMapper jobsetMapper;
    @Resource
    private AlarmCarMapper alarmCarMapper;
    @Resource
    private CarViolationMapper carViolationMapper;
    @Resource
    private StateOilDeviceMapper stateOilDeviceMapper;

    @Override
    public PageVo carPlanWorkCount(CarPlanCarTo carPlanCarTo) {

//        QueryWrapper<AlarmCar> alarmCarQueryWrapper = new QueryWrapper<>();
//        alarmCarQueryWrapper.lambda().between(AlarmCar::getAlarmTime, carPlanCarTo.getStartDate(), carPlanCarTo.getEndDate());
//        List<AlarmCar> alarmCarList = alarmCarMapper.selectList(alarmCarQueryWrapper);
//        Map<String, List<AlarmCar>> alarmCarMap = alarmCarList.stream().collect(Collectors.groupingBy(AlarmCar::getCarId));

        QueryWrapper<StateOilDevice> stateOilDeviceQueryWrapper = new QueryWrapper<>();
        stateOilDeviceQueryWrapper.lambda().between(StateOilDevice::getDeviceDate, carPlanCarTo.getStartDate(), carPlanCarTo.getEndDate());
        List<StateOilDevice> stateOilDeviceList = stateOilDeviceMapper.selectList(stateOilDeviceQueryWrapper);
        Map<String, List<StateOilDevice>> stateOilDeviceMap = stateOilDeviceList.stream().collect(Collectors.groupingBy(StateOilDevice::getCarId));

//        QueryWrapper<CarViolation> carViolationQueryWrapper = new QueryWrapper<>();
//        carViolationQueryWrapper.lambda().between(CarViolation::getTime, carPlanCarTo.getStartDate(), carPlanCarTo.getEndDate());
//        List<CarViolation> carViolationList = carViolationMapper.selectList(carViolationQueryWrapper);
//        Map<String, List<CarViolation>> carViolationMap = carViolationList.stream().collect(Collectors.groupingBy(CarViolation::getCarId));

        Map<String, SysDepart> sysDepartMap = sysDepartMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.groupingBy(SysDepart::getId,
                Collectors.collectingAndThen(Collectors.toList(), sysDeparts -> sysDeparts.get(0))));

        Jobset jobset = jobsetMapper.selectById(3);

        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(carPlanCarTo.getDepartmentId())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getDepartmentId, carPlanCarTo.getDepartmentId());
        }
        if (StringUtils.isNotEmpty(carPlanCarTo.getCarId())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carPlanCarTo.getCarId());
        }
        if (StringUtils.isNotEmpty(carPlanCarTo.getCarTypeName())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarType, carPlanCarTo.getCarTypeName());
        }
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 2);
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carPlanCarTo.getStartDate(), carPlanCarTo.getEndDate());
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        Map<String, List<RptCarWork>> rptCarWorkByCarId = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));

        List<CarPlanCarCountVo> carPlanCarCountVoList = new ArrayList<>();
        for (String carId : rptCarWorkByCarId.keySet()) {
            CarPlanCarCountVo carPlanCarCountVo = new CarPlanCarCountVo();

            BigDecimal workMileage = BigDecimal.ZERO;
            BigDecimal workTime = BigDecimal.ZERO;
            BigDecimal workOil = BigDecimal.ZERO;

            BigDecimal onGuardWorkTime = BigDecimal.ZERO;
            BigDecimal onGuardMileage = BigDecimal.ZERO;
            BigDecimal onGuardOil = BigDecimal.ZERO;
            BigDecimal planningMileage = BigDecimal.ZERO;

            Integer completeDays = 0;

            List<RptCarWork> rptCarWorkList1 = rptCarWorkByCarId.get(carId);

            BigDecimal travelMileage = BigDecimal.ZERO;
            BigDecimal travelOil = BigDecimal.ZERO;
            BigDecimal travelTime = BigDecimal.ZERO;

            List<StateOilDevice> stateOilDeviceList1 = stateOilDeviceMap.get(carId);
            if (null != stateOilDeviceList1) {
                for (StateOilDevice stateOilDevice : stateOilDeviceList1) {
                    travelMileage = travelMileage.add((null == stateOilDevice.getClockMileage() ? BigDecimal.ZERO : stateOilDevice.getClockMileage()));
                    travelOil = travelOil.add((null == stateOilDevice.getUseOilNum() ? BigDecimal.ZERO : stateOilDevice.getUseOilNum()));
                    travelTime = travelTime.add(null == stateOilDevice.getMoveSecondSum() ? BigDecimal.ZERO : stateOilDevice.getMoveSecondSum());
                }
            }


            //    行驶里程、行驶时长、行驶油耗、在岗里程、在岗时长、在岗油耗、作业里程、作业时长、作业油耗
            if (null != rptCarWorkList1) {
                Map<String, List<RptCarWork>> rptCarWorkByPlanIdMap = rptCarWorkList1.stream().collect(Collectors.groupingBy(RptCarWork::getPlanId));
                for (String planId : rptCarWorkByPlanIdMap.keySet()) {

                    List<RptCarWork> rptCarWorkList2 = rptCarWorkByPlanIdMap.get(planId);
                    if (null != rptCarWorkList2) {

                        Map<String, List<RptCarWork>> rptCarWorkByWorkDate = rptCarWorkList2.stream().collect(Collectors.groupingBy(RptCarWork::getWorkDate));
                        for (String workDate : rptCarWorkByWorkDate.keySet()) {

                            Boolean isFinish = true;

                            List<RptCarWork> rptCarWorkList3 = rptCarWorkByWorkDate.get(workDate);
                            if (null != rptCarWorkList3) {
                                for (RptCarWork rptCarWork : rptCarWorkList3) {

                                    carPlanCarCountVo.setCarId(rptCarWork.getCarId());
                                    carPlanCarCountVo.setCarNumber(rptCarWork.getCarNum());
                                    SysDepart sysDepart = sysDepartMap.get(rptCarWork.getDepartmentId());
                                    if (null != sysDepart) {
                                        carPlanCarCountVo.setDepartmentName(sysDepart.getDepartName());
                                    }
                                    carPlanCarCountVo.setCarType(rptCarWork.getCarType());


                                    planningMileage = planningMileage.add(null == rptCarWork.getAllProgress() ? BigDecimal.ZERO : new BigDecimal(rptCarWork.getAllProgress()));

                                    workOil = workOil.add(null == rptCarWork.getWorkOil() ? BigDecimal.ZERO : rptCarWork.getWorkOil());
                                    workTime = workTime.add(null == rptCarWork.getWorkTime() ? BigDecimal.ZERO : rptCarWork.getWorkTime());
                                    workMileage = workMileage.add(null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage());

                                    onGuardOil = onGuardOil.add(null == rptCarWork.getOnGuardOil() ? BigDecimal.ZERO : rptCarWork.getOnGuardOil());
                                    onGuardWorkTime = onGuardWorkTime.add(null == rptCarWork.getOnGuardTime() ? BigDecimal.ZERO : rptCarWork.getOnGuardTime());
                                    onGuardMileage = onGuardMileage.add(null == rptCarWork.getOnGuardMileage() ? BigDecimal.ZERO : rptCarWork.getOnGuardMileage());


                                    BigDecimal singleProgress = (rptCarWork.getTangCi() == null || rptCarWork.getTangCi() == 0) ? BigDecimal.ZERO : new BigDecimal(rptCarWork.getAllProgress()).divide(new BigDecimal(rptCarWork.getTangCi()), BigDecimal.ROUND_HALF_UP);

                                    BigDecimal requireSingleProgress = new BigDecimal(jobset.getKmOkThreshold()).multiply(singleProgress).divide(new BigDecimal(100), BigDecimal.ROUND_HALF_UP);

                                    int thisTangCi = (requireSingleProgress == null || requireSingleProgress.equals(BigDecimal.ZERO)) ? 0 : (null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage()).divide(requireSingleProgress, BigDecimal.ROUND_FLOOR).intValue();


                                    if (thisTangCi < rptCarWork.getTangCi()) {
                                        isFinish = false;
                                    }

                                }
                            }
                            if (isFinish) {
                                completeDays++;
                            }
                        }
                        carPlanCarCountVo.setPlanDays(rptCarWorkByWorkDate.keySet().size());
                    }
                }
            }

            carPlanCarCountVo.setWorkMileage(workMileage.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
            carPlanCarCountVo.setWorkOil(workOil);
            carPlanCarCountVo.setWorkTime(Tools.secondToTime(workTime.longValue()));

            carPlanCarCountVo.setOnGuardMileage(onGuardMileage.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
            carPlanCarCountVo.setOnGuardOil(onGuardOil);
            carPlanCarCountVo.setOnGuardTime(Tools.secondToTime(onGuardWorkTime.longValue()));


            carPlanCarCountVo.setEmptyMileage((onGuardMileage.subtract(workMileage)).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
            carPlanCarCountVo.setEmptyTime(Tools.secondToTime((onGuardWorkTime.subtract(workTime)).longValue()));
            carPlanCarCountVo.setEmptyOil(onGuardOil.subtract(workOil));

            carPlanCarCountVo.setTravelOil(travelOil);
            carPlanCarCountVo.setTravelMileage(travelMileage);
            carPlanCarCountVo.setTravelTime(Tools.secondToTime((travelTime.divide(new BigDecimal(1000))).longValue()));

            carPlanCarCountVo.setCompleteDays(completeDays);
            carPlanCarCountVo.setUnCompleteDays(carPlanCarCountVo.getPlanDays() - carPlanCarCountVo.getCompleteDays());

            carPlanCarCountVoList.add(carPlanCarCountVo);
        }
        return Tools.page(carPlanCarTo.getPage(), carPlanCarTo.getSize(), carPlanCarCountVoList);
    }

    @Override
    public PageVo carPlanWorkDetail(CarPlanCarTo carPlanCarTo) {

        Jobset jobset = jobsetMapper.selectById(3);

        Map<String, SysDepart> sysDepartMap = sysDepartMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.groupingBy(SysDepart::getId,
                Collectors.collectingAndThen(Collectors.toList(), sysDeparts -> sysDeparts.get(0))));

        QueryWrapper<StateOilDevice> stateOilDeviceQueryWrapper = new QueryWrapper<>();
        stateOilDeviceQueryWrapper.lambda().eq(StateOilDevice::getCarId, carPlanCarTo.getCarId());
        stateOilDeviceQueryWrapper.lambda().between(StateOilDevice::getDeviceDate, carPlanCarTo.getStartDate(), carPlanCarTo.getEndDate());
        List<StateOilDevice> stateOilDeviceList = stateOilDeviceMapper.selectList(stateOilDeviceQueryWrapper);

        BigDecimal travelMileage = BigDecimal.ZERO;
        BigDecimal travelOil = BigDecimal.ZERO;
        BigDecimal travelTime = BigDecimal.ZERO;

        for (StateOilDevice stateOilDevice : stateOilDeviceList) {
            travelMileage = travelMileage.add((null == stateOilDevice.getClockMileage() ? BigDecimal.ZERO : stateOilDevice.getClockMileage()));
            travelOil = travelOil.add((null == stateOilDevice.getUseOilNum() ? BigDecimal.ZERO : stateOilDevice.getUseOilNum()));
            travelTime = travelTime.add(null == stateOilDevice.getMoveSecondSum() ? BigDecimal.ZERO : stateOilDevice.getMoveSecondSum());

        }

        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carPlanCarTo.getCarId());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 2);
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carPlanCarTo.getStartDate(), carPlanCarTo.getEndDate());
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
//        车牌号、所属机构、车辆类型、计划名称、网格名称、排班日期、作业考勤段（作业班组考勤时间段的个数）、
//        完成状态、设定趟次、作业趟次、出车时间（当日第一次接收到车辆定位的时间）、收车时间（当日最后一次接收到车辆定位的时间）、
//        规划里程、行驶里程、作业里程、空驶里程、报警次数、违规次数、耗油量、作业油耗
        List<CarPlanCarDetailVo> carPlanCarDetailVoList = new ArrayList<>();


        Map<String, List<RptCarWork>> rptCarWorkByPlanIdMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getPlanId));
        for (String planId : rptCarWorkByPlanIdMap.keySet()) {

            BigDecimal workOil = BigDecimal.ZERO;
            BigDecimal workMileage = BigDecimal.ZERO;
            BigDecimal workTime = BigDecimal.ZERO;
            BigDecimal onGuardOil = BigDecimal.ZERO;
            BigDecimal onGuardMileage = BigDecimal.ZERO;
            BigDecimal onGuardWorkTime = BigDecimal.ZERO;
            BigDecimal planningMileage = BigDecimal.ZERO;


            Integer tangCi = 0;
            Integer workTangCi = 0;
            Integer completeDays = 0;

            List<RptCarWork> rptCarWorkList1 = rptCarWorkByPlanIdMap.get(planId);
            if (null != rptCarWorkList1) {
                CarPlanCarDetailVo carPlanCarDetailVo = new CarPlanCarDetailVo();


                Map<String, List<RptCarWork>> rptCarWorkByWorkDate = rptCarWorkList1.stream().collect(Collectors.groupingBy(RptCarWork::getWorkDate));
                for (String workDate : rptCarWorkByWorkDate.keySet()) {

                    Boolean isFinish = true;

                    List<RptCarWork> rptCarWorkList2 = rptCarWorkByWorkDate.get(workDate);
                    if (null != rptCarWorkList2) {
                        for (RptCarWork rptCarWork : rptCarWorkList2) {
                            carPlanCarDetailVo.setCarId(rptCarWork.getCarId());
                            carPlanCarDetailVo.setPlanId(rptCarWork.getPlanId());
                            carPlanCarDetailVo.setCarNumber(rptCarWork.getCarNum());
                            SysDepart sysDepart = sysDepartMap.get(rptCarWork.getDepartmentId());
                            if (null != sysDepart) {
                                carPlanCarDetailVo.setDepartmentName(sysDepart.getDepartName());
                            }
                            carPlanCarDetailVo.setCarType(rptCarWork.getCarType());
                            carPlanCarDetailVo.setPlanName(rptCarWork.getPlanName());
                            carPlanCarDetailVo.setGridName(rptCarWork.getGridName());
                            carPlanCarDetailVo.setStatus(rptCarWork.getStatus());


                            planningMileage = planningMileage.add(null == rptCarWork.getAllProgress() ? BigDecimal.ZERO : new BigDecimal(rptCarWork.getAllProgress()));

                            workOil = workOil.add(null == rptCarWork.getWorkOil() ? BigDecimal.ZERO : rptCarWork.getWorkOil());
                            workTime = workTime.add(null == rptCarWork.getWorkTime() ? BigDecimal.ZERO : rptCarWork.getWorkTime());
                            workMileage = workMileage.add(null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage());

                            onGuardOil = onGuardOil.add(null == rptCarWork.getOnGuardOil() ? BigDecimal.ZERO : rptCarWork.getOnGuardOil());
                            onGuardWorkTime = onGuardWorkTime.add(null == rptCarWork.getOnGuardTime() ? BigDecimal.ZERO : rptCarWork.getOnGuardTime());
                            onGuardMileage = onGuardMileage.add(null == rptCarWork.getOnGuardMileage() ? BigDecimal.ZERO : rptCarWork.getOnGuardMileage());


                            BigDecimal singleProgress = (rptCarWork.getTangCi() == null || rptCarWork.getTangCi() == 0) ? BigDecimal.ZERO : new BigDecimal(rptCarWork.getAllProgress()).divide(new BigDecimal(rptCarWork.getTangCi()), BigDecimal.ROUND_HALF_UP);

                            BigDecimal requireSingleProgress = new BigDecimal(jobset.getKmOkThreshold()).multiply(singleProgress).divide(new BigDecimal(100), BigDecimal.ROUND_HALF_UP);

                            int thisTangCi = (requireSingleProgress == null || requireSingleProgress.equals(BigDecimal.ZERO)) ? 0 : (null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage()).divide(requireSingleProgress, BigDecimal.ROUND_FLOOR).intValue();


                            if (thisTangCi < rptCarWork.getTangCi()) {
                                isFinish = false;
                            }

                            tangCi = tangCi + rptCarWork.getTangCi();

                            workTangCi = workTangCi + thisTangCi;
                        }
                    }
                    if (isFinish) {
                        completeDays++;
                    }
                }

                carPlanCarDetailVo.setPlanMileage(planningMileage.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
                carPlanCarDetailVo.setWorkMileage(workMileage.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
                carPlanCarDetailVo.setWorkOil(workOil);
                carPlanCarDetailVo.setWorkTime(Tools.secondToTime(workTime.longValue()));

                carPlanCarDetailVo.setOnGuardMileage(onGuardMileage.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
                carPlanCarDetailVo.setOnGuardOil(onGuardOil);
                carPlanCarDetailVo.setOnGuardTime(Tools.secondToTime(onGuardWorkTime.longValue()));


                carPlanCarDetailVo.setEmptyMileage((onGuardMileage.subtract(workMileage)).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
                carPlanCarDetailVo.setEmptyTime(Tools.secondToTime((onGuardWorkTime.subtract(workTime)).longValue()));
                carPlanCarDetailVo.setEmptyOil(onGuardOil.subtract(workOil));

                carPlanCarDetailVo.setTravelOil(travelOil);
                carPlanCarDetailVo.setTravelMileage(travelMileage);
                carPlanCarDetailVo.setTravelTime(Tools.secondToTime((travelTime.divide(new BigDecimal(1000))).longValue()));

                carPlanCarDetailVo.setSetTangCi(tangCi);
                carPlanCarDetailVo.setWorkTangCi(workTangCi);
                carPlanCarDetailVo.setCompleteDays(completeDays);
                carPlanCarDetailVo.setPlanDays(rptCarWorkByWorkDate.keySet().size());

                carPlanCarDetailVoList.add(carPlanCarDetailVo);
            }
        }

        return Tools.page(carPlanCarTo.getPage(), carPlanCarTo.getSize(), carPlanCarDetailVoList);
    }

    @Override
    public CarPlanCarByAppCountVo trashPickupWorkPlanCount() {
        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 3);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        CarPlanCarByAppCountVo carPlanCarByAppCountVo = new CarPlanCarByAppCountVo();

        List<RptCarWork> rptCarWorkListByStatus0 = rptCarWorkList.stream().filter(rptCarWork -> null != rptCarWork.getStatus() && rptCarWork.getStatus() == 0).collect(Collectors.toList());
        List<RptCarWork> rptCarWorkListByStatus1 = rptCarWorkList.stream().filter(rptCarWork -> null != rptCarWork.getStatus() && rptCarWork.getStatus() == 1).collect(Collectors.toList());
        List<RptCarWork> rptCarWorkListByStatus2 = rptCarWorkList.stream().filter(rptCarWork -> null != rptCarWork.getStatus() && rptCarWork.getStatus() == 2).collect(Collectors.toList());
        List<RptCarWork> rptCarWorkListByStatus3 = rptCarWorkList.stream().filter(rptCarWork -> null != rptCarWork.getStatus() && rptCarWork.getStatus() == 3).collect(Collectors.toList());

        carPlanCarByAppCountVo.setUnStartNumber(rptCarWorkListByStatus0.size());
        carPlanCarByAppCountVo.setWorkIngNumber(rptCarWorkListByStatus1.size());
        carPlanCarByAppCountVo.setUnCompleteNumber(rptCarWorkListByStatus2.size());
        carPlanCarByAppCountVo.setCompleteNumber(rptCarWorkListByStatus3.size());
        carPlanCarByAppCountVo.setPlanNumber(rptCarWorkList.size());
        return carPlanCarByAppCountVo;

    }

    @Override
    public List<CarPlanCarByAppVo> trashPickupWorkPlanList() {

        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 3);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        List<CarPlanCarByAppVo> carPlanCarByAppVoList = new ArrayList<>();
        for (RptCarWork rptCarWork : rptCarWorkList) {
            CarPlanCarByAppVo carPlanCarByAppVo = new CarPlanCarByAppVo();
            carPlanCarByAppVo.setCarId(rptCarWork.getCarId());
            carPlanCarByAppVo.setCarType(rptCarWork.getCarType());
            carPlanCarByAppVo.setCarNumber(rptCarWork.getCarNum());
            carPlanCarByAppVo.setPlanName(rptCarWork.getPlanName());
            carPlanCarByAppVo.setShiftName(rptCarWork.getShiftName());
            carPlanCarByAppVo.setCompletePercent(rptCarWork.getCompletePercent());
            carPlanCarByAppVo.setStatus(rptCarWork.getStatus());
            carPlanCarByAppVo.setPlanStartName(rptCarWork.getPlanStartTime());
            carPlanCarByAppVo.setPlanEndName(rptCarWork.getPlanEndTime());
            carPlanCarByAppVoList.add(carPlanCarByAppVo);
        }
        return carPlanCarByAppVoList;
    }


    @Override
    public PageVo carPlanWorkDetailByCar(CarPlanCarTo carPlanCarTo) {

        List<CarPlanCarDetailByCarVo> carPlanCarDetailByCarVoList = new ArrayList<>();
        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().orderByAsc(RptCarWork::getWorkDate);
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carPlanCarTo.getCarId());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getPlanId, carPlanCarTo.getPlanId());
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carPlanCarTo.getStartDate(), carPlanCarTo.getEndDate());
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        QueryWrapper<StateOilDevice> stateOilDeviceQueryWrapper = new QueryWrapper<>();
        stateOilDeviceQueryWrapper.lambda().eq(StateOilDevice::getCarId, carPlanCarTo.getCarId());
        stateOilDeviceQueryWrapper.lambda().between(StateOilDevice::getDeviceDate, carPlanCarTo.getStartDate(), carPlanCarTo.getEndDate());
        List<StateOilDevice> stateOilDeviceList = stateOilDeviceMapper.selectList(stateOilDeviceQueryWrapper);
        Map<Date, List<StateOilDevice>> stateOilDeviceByDateMap = stateOilDeviceList.stream().collect(Collectors.groupingBy(StateOilDevice::getDeviceDate));


        for (RptCarWork rptCarWork : rptCarWorkList) {
            CarPlanCarDetailByCarVo carPlanCarDetailByCarVo = new CarPlanCarDetailByCarVo();

            carPlanCarDetailByCarVo.setWorkDate(rptCarWork.getWorkDate());
            carPlanCarDetailByCarVo.setShiftName(rptCarWork.getShiftName());
            carPlanCarDetailByCarVo.setPlanStartTime(rptCarWork.getPlanStartTime());
            carPlanCarDetailByCarVo.setPlanEndTime(rptCarWork.getPlanEndTime());
            carPlanCarDetailByCarVo.setWorkStartTime(rptCarWork.getWorkStartTime());
            carPlanCarDetailByCarVo.setWorkEndTime(rptCarWork.getWorkEndTime());
            carPlanCarDetailByCarVo.setWorkTime(Tools.secondToTime((null == rptCarWork.getWorkTime() ? 0 : rptCarWork.getWorkTime()).longValue()));
            carPlanCarDetailByCarVo.setCompletePercent(rptCarWork.getCompletePercent());
            carPlanCarDetailByCarVo.setStatus(rptCarWork.getStatus());
            List<StateOilDevice> stateOilDeviceList1 = stateOilDeviceByDateMap.get(DateUtil.parse(rptCarWork.getWorkDate(), "yyyy-MM-dd"));
            if (null != stateOilDeviceList1) {
                for (StateOilDevice stateOilDevice : stateOilDeviceList1) {
                    carPlanCarDetailByCarVo.setCarStartTime(stateOilDevice.getStartDate());
                    carPlanCarDetailByCarVo.setCarEndTime(stateOilDevice.getEndDate());
                }
            }
            carPlanCarDetailByCarVoList.add(carPlanCarDetailByCarVo);
        }
        return Tools.page(carPlanCarTo.getPage(), carPlanCarTo.getSize(), carPlanCarDetailByCarVoList);
    }
}
